package kr.co.hk.depart;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import kr.co.hk.R;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.WebView;
import android.widget.Toast;

public class ShinDetail extends Activity {
	final Activity activity = this;
	private WebView wv;
	private String url;
	private String address;
	private ProgressDialog progressDialog;
	final private int PROGRESS_DIALOG = 0;
	private boolean pageEx = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shin_detail);

		Bundle extra = getIntent().getExtras();
		url = extra.getString("url");

		wv = (WebView) findViewById(R.id.shinWebView);
		wv.clearCache(true);
		wv.getSettings().setSaveFormData(false);
		wv.getSettings().setBuiltInZoomControls(true);

		mainProcessing();

	}

	private Handler chdHandler = new Handler();

	private void mainProcessing() {
		 pageEx = false;
		showDialog(PROGRESS_DIALOG);
		Thread childThread = new Thread(null, dobackgroundThreadProcessing,
				"Backgroud");
		childThread.start();
	}

	private Runnable dobackgroundThreadProcessing = new Runnable() {

		@Override
		public void run() {
			backgroudnThreadProcessing();
		}
	};

	private void backgroudnThreadProcessing() {
		getDateList(url);
		chdHandler.post(doUpdateGUI);
	}

	private Runnable doUpdateGUI = new Runnable() {

		@Override
		public void run() {
			updateGUI();
		}
	};

	// gui code
	private void updateGUI() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		progressDialog.dismiss();
		ShinDetail.this.removeDialog(PROGRESS_DIALOG);
		if (pageEx) {
			excePro();
		} else {
		
			wv.loadUrl(address);
		}
	}

	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case PROGRESS_DIALOG:

			progressDialog = new ProgressDialog(this);
			progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressDialog.setMessage("Now loading..");
			progressDialog.setIndeterminate(true);
			progressDialog.setCancelable(false);
			return progressDialog;

		}
		return null;
	}

	public void getDateList(String addr) {
		try {
			URL url = new URL(addr);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			if (conn != null) {
				conn.setConnectTimeout(10000);
				conn.setReadTimeout(10000);
				conn.setUseCaches(false);
				if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					BufferedReader br = new BufferedReader(
							new InputStreamReader(conn.getInputStream(),
									"euc-kr"));
					ArrayList<String> list = new ArrayList<String>();
					String msg = "";
					while ((msg = br.readLine()) != null) {
						list.add(msg.trim() + "\n");
					}

					for (int i = 0; i < list.size(); i++) {

						String line = list.get(i);
						String reStr = line;
						int firstIndex = 0;
						int lastIndex = 0;

						if (line.contains("class=\"tblCNT")) {
							reStr = list.get(i + 1);
							if (reStr.contains("alt")) {

								firstIndex = reStr.indexOf("\"http");
								lastIndex = reStr.indexOf("width");
								address = reStr.substring(firstIndex + 1,
										lastIndex - 2);
							}

							firstIndex = reStr.indexOf("\"http");
							lastIndex = reStr.indexOf("\" ");
							address = reStr
									.substring(firstIndex + 1, lastIndex);

						}
					}
					br.close();
				}
				conn.disconnect();

			}

		} catch (Exception ex) {
			pageEx = true;
			ex.printStackTrace();

		}
	}

	private void excePro() {
		toast("페이지 로딩시 오류가  있습니다. \n 네트워크 확인 후 다시 시도 하세요!");
		onBackPressed();
	}

	private void toast(String text) {
		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
	}

}
